package com.bigflexlib.ds.op
{
	import com.bigflexlib.ds.DataStructureEvent;
	import com.bigflexlib.ds.container.ContainerDiff;
	import com.bigflexlib.ds.container.IRContainer;
	import com.bigflexlib.ds.container.IWContainer;

	public class AbstractContainerFilterOp extends AbstractContainerOp
	{
		
		
		private var iFilterFunction:Function;
		
		private var oContainer:IWContainer;
		
		
		public function AbstractContainerFilterOp(iContainer:IRContainer, oContainer:IWContainer, iFilterFunction:Function)
		{
			
			
			super(iContainer);
			
			this.iFilterFunction = iFilterFunction;
			
			this.oContainer = oContainer;
			
			batch( );
			
		}
		
		
		
		
		private function batch( ):void
		{
		
			oContainer.autoUpdateFlag.disable( );
			
				oContainer.clear();

				iContainer.walk(
				
					function( e:*, multiplicity:int ):void
					{
						if ( test( e ) )
						{
							m( e, multiplicity );		
						}
					}
				
				 );
							

			oContainer.autoUpdateFlag.enable( );	
		
		}
		



		override public function dispose():void
		{
			
			super.dispose();
			
			iFilterFunction = null;
			
			oContainer = null;
			
		}


		
		
		override protected function iContainer_change( event:DataStructureEvent ):void
		{
			
			oContainer.autoUpdateFlag.disable( );				
			
		
				var diff:ContainerDiff = event.diff as ContainerDiff;
				
				diff.walk(
					
					function( e:*, multiplicity:int ):void
					{
						if ( test( e ) )
						{
							m( e, multiplicity );		
						}
					}			
				
				
				 );
		
					
			oContainer.autoUpdateFlag.enable( );
		
		
		}		
		
		
		
		
		

		private function test( e:* ):Boolean
		{
			return ( iFilterFunction.call( null, e ) == true );
		}
		



		
		// abstract
		protected function m( e:*, m:int ):void
		{
		}
		
		
		
		
		
		
	}
}